package com.qianji.petmall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianji.common.constant.WareConstant;
import com.qianji.common.utils.PageUtils;
import com.qianji.common.utils.Query;
import com.qianji.petmall.ware.dao.PurchaseDao;
import com.qianji.petmall.ware.entity.PurchaseDetailEntity;
import com.qianji.petmall.ware.entity.PurchaseEntity;
import com.qianji.petmall.ware.service.PurchaseDetailService;
import com.qianji.petmall.ware.service.PurchaseService;
import com.qianji.petmall.ware.service.WareSkuService;
import com.qianji.petmall.ware.vo.DoneItemsVo;
import com.qianji.petmall.ware.vo.DoneVO;
import com.qianji.petmall.ware.vo.MergeVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    private PurchaseDetailService purchaseDetailService;
    @Autowired
    private WareSkuService wareSkuService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<PurchaseEntity> purchaseEntityQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(key)){
            purchaseEntityQueryWrapper.and(w->{
                w.eq("id",key).or().like("assignee_name",key);
            });
        }
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                purchaseEntityQueryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void queryPageMerge(MergeVo mergeVo) {
        Long purchaseId = mergeVo.getPurchaseId();
        if (purchaseId != null){
            List<Long> items = mergeVo.getItems();
            List<PurchaseDetailEntity> collect = items.stream().map(item -> {
                PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
                purchaseDetailEntity.setPurchaseId(purchaseId);
                purchaseDetailEntity.setStatus(WareConstant.StatusPurchase.ASSIGNED.getCode());
                purchaseDetailEntity.setId(item);
                return purchaseDetailEntity;
            }).collect(Collectors.toList());

            purchaseDetailService.updateBatchById(collect);

            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setUpdateTime(new Date());
            purchaseEntity.setId(purchaseId);
            this.updateById(purchaseEntity);
        }
    }

    @Override
    public void received(List<Long> list) {
        List<PurchaseEntity> collect = list.stream().map(item -> {
            PurchaseEntity byId = this.getById(item);

            return byId;
        }).filter(item->{
            if (item.getStatus() == WareConstant.StatusPurchase.CREATE.getCode() || item.getStatus() == WareConstant.StatusPurchase.ASSIGNED.getCode()){
                return true;
            }else {
                return false;
            }
        }).map(item->{
            item.setStatus(WareConstant.Status.PURCHASE.getCode());
            item.setUpdateTime(new Date());
            return item;
        }).collect(Collectors.toList());

        this.updateBatchById(collect);

        collect.forEach(item->{
            List<PurchaseDetailEntity> detailEntityList = purchaseDetailService.getpurchaseId(item.getId());
            List<PurchaseDetailEntity> collect1 = detailEntityList.stream().map(items -> {
                PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
                purchaseDetailEntity.setStatus(WareConstant.StatusPurchase.PURCHASE.getCode());
                purchaseDetailEntity.setId(items.getId());
                return purchaseDetailEntity;
            }).collect(Collectors.toList());
            purchaseDetailService.updateBatchById(collect1);
        });
    }

    @Override
    public PageUtils queryPageNone(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().in("status",0,1)
        );
        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void Updatedone(DoneVO doneVO) {
        Long id = doneVO.getId();
        //改变采购项的状态
        List<DoneItemsVo> items = doneVO.getItems();
        List<PurchaseDetailEntity> arrayList = new ArrayList<>();
        items.forEach(item->{
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            if (item.getStatus() == WareConstant.StatusPurchase.FAILURE.getCode()) {
                purchaseDetailEntity.setStatus(item.getStatus());
            } else {
                purchaseDetailEntity.setStatus(WareConstant.StatusPurchase.STOCKS.getCode());
                //改变库存
                PurchaseDetailEntity byId = purchaseDetailService.getById(item.getItemId());
                Long skuId = byId.getSkuId();
                Integer skuNum = byId.getSkuNum();
                Long wareId = byId.getWareId();
                wareSkuService.updateWork(skuId,skuNum,wareId);
            }
            purchaseDetailEntity.setId(item.getItemId());
            arrayList.add(purchaseDetailEntity);
        });
        purchaseDetailService.updateBatchById(arrayList);

        //改变采购单状态
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(id);
        purchaseEntity.setUpdateTime(new Date());
        purchaseEntity.setStatus(WareConstant.Status.STOCKS.getCode());
        this.updateById(purchaseEntity);

    }

}